﻿using System.Text;
using Shop.RemoteModel;
using Shop.RemoteModel.LogisticsConfig.Model;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.Area;

namespace Shop.Collect.Linq
{
    internal static class LogisticsLinqHelper
    {
        public static string GetLogisticsShow ( this LogisticsConfigDto config )
        {
            string area = AreaHelper.GetAreaName(config.AreaId);
            StringBuilder str = new StringBuilder();
            _ = str.AppendFormat("地区：{0}，物流费用：{1}元，计费方式：{2}。\n", area, config.LogisticsPrice, config.ValuationMode);
            string unit = "kg";
            if ( config.ValuationMode == ValuationMode.计重 )
            {
                _ = str.AppendFormat("首重：{0}kg，每增加{1}kg，增加运费{2}元。\n", config.FirstVal, config.ContinueVal, config.ContinuePrice);
            }
            else if ( config.ValuationMode == ValuationMode.计体积 )
            {
                _ = str.AppendFormat("首重：{0}kg，每增加{1}kg，增加运费{2}元。计重换算公式：体积/{3}=kg。\n", config.FirstVal, config.ContinueVal, config.ContinuePrice, config.CcValue);
            }
            if ( config.FreeValue != 0 && config.FreePrice != 0 )
            {
                _ = str.AppendFormat("满{0}元或满{1}{2}包邮。", config.FreePrice, config.FreeValue, unit);
            }
            else if ( config.FreePrice != 0 )
            {
                _ = str.AppendFormat("满{0}元包邮。", config.FreePrice);
            }
            else if ( config.FreeValue != 0 )
            {
                _ = str.AppendFormat("满{0}{1}包邮。", config.FreeValue, unit);
            }
            return str.ToString();
        }

        public static bool CheckIsUsable ( this LogisticsConfigDto config, CalculateLogistics param )
        {
            if ( config.ValuationMode == ValuationMode.计重和体积 )
            {
                return param.Volume.HasValue;
            }
            else if ( config.ValuationMode == ValuationMode.计重 )
            {
                return param.Weight.HasValue;
            }
            else
            {
                return config.ValuationMode == ValuationMode.固定金额;
            }
        }
        public static decimal Calculate ( this LogisticsConfigDto config, CalculateLogistics param )
        {
            if ( config.FreePrice != 0 && param.TotalPrice >= config.FreePrice )
            {
                return 0;
            }
            else if ( config.ValuationMode == ValuationMode.计重 )
            {
                return _Calculate(config, param.Weight.Value);
            }
            else if ( config.ValuationMode == ValuationMode.计重和体积 )
            {
                return _CalculateBy(config, param.Volume.Value, param.Weight.Value);
            }
            else if ( config.ValuationMode == ValuationMode.计体积 )
            {
                return _CalculateByVolume(config, param.Volume.Value);
            }
            return config.LogisticsPrice;
        }
        private static decimal _CalculateByVolume ( LogisticsConfigDto config, decimal volume )
        {
            decimal val = volume / config.CcValue;
            return _Calculate(config, val);
        }
        private static decimal _CalculateBy ( LogisticsConfigDto config, decimal volume, decimal weight )
        {
            decimal val = volume / config.CcValue;
            return _Calculate(config, val > weight ? val : weight);
        }
        private static decimal _Calculate ( LogisticsConfigDto config, decimal num )
        {
            if ( config.FreeValue != 0 && config.FreeValue <= num )
            {
                return 0;
            }
            decimal price = config.LogisticsPrice;
            if ( num > config.FirstVal )
            {
                price += ( ( num - config.FirstVal ) / config.ContinueVal * config.ContinuePrice );
            }
            return Math.Round(price, 1);
        }

        private const int _DefSort = 100;
        private static readonly Dictionary<AreaType, int> _AreaSort = new Dictionary<AreaType, int>()
        {
            {AreaType.区县,1 },
            {AreaType.城市,2 },
            {AreaType.省份,3 },
            {AreaType.国家,4 },
            {AreaType.未知,5 },
        };
        public static int Sort ( this LogisticsConfigDto config )
        {
            return _AreaSort[config.AreaType];
        }
    }
}
